/**
 * 
 */
package com.ctg.payroll2.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author windy
 *
 */
public class CalDate {
	public static Calendar getCalendar(String year, String month, String day){
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
		calendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
		calendar.set(Calendar.YEAR,Integer.parseInt(year));
		
		return calendar;
	}
	
	public static Calendar getCalendar(String year, String month, String day, String hour, String minute){
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(Calendar.MINUTE,Integer.parseInt(minute));
		calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hour));
		calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
		calendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
		calendar.set(Calendar.YEAR,Integer.parseInt(year));
		
		return calendar;
	}
	
	/**
	 * @param date 日期字符串，格式为 yyyy-MM-dd
	 * @return 字符串转换为日期
	 */
	public static Date getDate(String date){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(date.substring(8)));
		calendar.set(Calendar.MONTH,Integer.parseInt(date.substring(5, 6))-1);
		calendar.set(Calendar.YEAR,Integer.parseInt(date.substring(0, 3)));

		return calendar.getTime();
	}
	
	/**
	 * @param format 日期格式
	 * @return 当前日期指定格式的字符串
	 */
	public static String getCurDate(String format){
		Calendar calendar = Calendar.getInstance();
		return getDate(calendar.getTime(), format);
	}
	
	/**
	 * @param date 日期
	 * @param format 格式
	 * @return 将日期格式化为指字格式的字符串
	 */
	public static String getDate(Date date, String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
	/**
	 * @param year 年份
	 * @param month 月份
	 * @return 指定年月的第一天日期字符串 yyyy-MM-dd
	 */
	public static String getMonthFirstDay(String year, String month){
		Calendar calendar = Calendar.getInstance();
		//获取下月第一天
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.set(Calendar.MONTH,Integer.parseInt(month) - 1);
		calendar.set(Calendar.YEAR,Integer.parseInt(year));
		
		return CalDate.getDate(calendar.getTime(), "yyyy-MM-dd");
	}
	
	/**
	 * @param year 年份
	 * @param month 月份
	 * @return 指定年月的最后一天日期字符串 yyyy-MM-dd
	 */
	public static String getMonthLastDay(String year, String month){
		Calendar calendar = Calendar.getInstance();
		//获取下月第一天
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.set(Calendar.MONTH,Integer.parseInt(month));
		calendar.set(Calendar.YEAR,Integer.parseInt(year));
		
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		
		return CalDate.getDate(calendar.getTime(), "yyyy-MM-dd");
	}
	
	/**
	 * @param year 年份
	 * @param month 月份
	 * @return 指定年月的下一月字符串 yyyyMM
	 */
	public static String getNextMonth(String year, String month){
		Calendar calendar = Calendar.getInstance();
		//获取下月第一天
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.set(Calendar.MONTH,Integer.parseInt(month));
		calendar.set(Calendar.YEAR,Integer.parseInt(year));
		
		return CalDate.getDate(calendar.getTime(), "yyyyMM");
	}
}
